<?php

namespace app\models;

use Yii;
use yii\db\Query;


class Repay
{
    public function census()
    {
        $listsql = "select a.id,a.store_id,a.account_code,a.money-a.already_money as df_money,b.store_name,c.business_type,UNIX_TIMESTAMP(DATE_ADD(FROM_UNIXTIME(a.dead_line,'%Y-%m-%d 00:00:00'),
INTERVAL 1 DAY)) as dead_line From yt_accounting_period a LEFT JOIN yt_trade_store b ON b.id=a.store_id LEFT JOIN yt_trade_infor c ON c.id=b.infor_id WHERE a.dead_line<" . time() . " AND a.status=0";
        $cardres = Yii::$app->db2->createCommand($listsql)->queryAll();
        return ['code' => 200, 'message' => 'OK', 'data' => $cardres];
    }

    public function detail()
    {
        $post = Yii::$app->request->post();
        if (!empty($post['yid'] && !empty($post['pid']))) {
            //用户信息
            $usersql = "select a.store_name,FROM_UNIXTIME(passing_time,'%Y-%m-%d') as passing_time,TIMESTAMPDIFF(day,FROM_UNIXTIME(passing_time,'%Y-%m-%d'),CURRENT_DATE ) AS time_cell,b.business_nature,b.business_grade,b.business_scale From bf_customer a left join bf_infor b ON b.id=a.infor_id WHERE a.yid=:yid";
            $userres = Yii::$app->db->createCommand($usersql)->bindValue(':yid', $post['yid'])->queryOne();
            //统计企业评分
            $gradesql="select COUNT(id) as num,SUM(money) as money From yt_accounting where period_id=:pid and status=0";
            $graderes = Yii::$app->db2->createCommand($gradesql)->bindValue(':pid', $post['pid'])->queryOne();
            $dead_num=Yii::$app->db2->createCommand("select COUNT(id) AS date_num From yt_accounting where DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(FROM_UNIXTIME(dead_line,'%Y-%m-%d')) and period_id=:pid and status=0")->bindValue(':pid', $post['pid'])->queryone();
            $graderes['dead_num']=$dead_num['date_num'];
            $userres['graderes']=$graderes;
            //代付信息
            $repaysql = "select a.account_code,a.money-a.already_money as df_money,COUNT(b.period_id) as num,a.months From yt_accounting_period a left join yt_accounting as b ON b.period_id=a.id WHERE a.id=:pid";
            $repayres = Yii::$app->db2->createCommand($repaysql)->bindValue(':pid', $post['pid'])->queryOne();
            //暂定参数
            $repayres['pay_type']='企业配件采购款';
            //采购清单列表
            $cgsql = "select a.money,c.order_number,c.parts_num,FROM_UNIXTIME(c.created_at,'%Y-%m-%d %H:%i:%s') as passing_time,c.order_type,d.store_name,(case c.order_type when '1' then '系统核定' when '2' then '自主采购' else order_type end) as order_type From yt_accounting a 
left join yt_purchase_order_detail b ON a.order_id=b.id 
left join yt_purchase_order c ON b.order_id=c.id
left join yt_trade_store d on d.id=b.store_id where a.period_id=:period_id group by b.id";
            $cgres = Yii::$app->db2->createCommand($cgsql)->bindValue(':period_id', $post['pid'])->queryAll();
        } else {
            return ['code' => 401, 'message' => '缺少参数', 'data' => ''];
        }
        $date = [
            'userinfo' => $userres,
            'repayinfo'=>$repayres,
            'cglist'=>$cgres
        ];
        return ['code' => 200, 'message' => 'OK', 'data' => $date];
    }
}
